<?php
/**
 * AMetro theme functions and definitions
 *
 * code by august 
 */

/**
 * wordpress 3.0 menu funcitions
 *
 * code by august 
 */

 function wordcut($str,$len,$add=true){
    /******************
    Author:August
    Date:2006-8-23
    Usage:Cut a string by its display width
    Parameters:{
    str: The string you want to cut.
    len: The display width of a string you want.(A alpha take one and a Chinese take two).
    add: Add a string '...' after the cuted string.
    }
    ******************/
    //Get the display width of the string.
    $i =0;
    $j =0;
    $str_width = 0;
    do{
            if(ord($str[$i]) > 224){
                    $str_width += 2;
                    $i += 3;
                }
            else if(ord($str[$i] > 192)){
                    $str_width += 2;
                    $i += 2;
                }
            else{
                    $str_width++;
                    $i++;
                }
        }while($i<strlen($str));
    //IF the display width is shorter than you want ,return the string.
    if($str_width < $len)
        {
            return $str;
        }
    else{
            $i = 0;
            $j = 0;
            $newword = '';
            do{
                    //If the character is a Chinese
                    if(ord($str[$i]) > 224){
                            $newword .= $str[$i].$str[$i+1].$str[$i+2];
                            $i = $i +3;
                            $j =$j + 2;
                        }
                    //If the character is a symble
                    else if(ord($str[$i] > 192)){
                            $newword .= $str[$i].$str[$i+1];
                            $i = $i + 2;
                            $j = $j + 2;
                        }
                    //If the character is a alpha
                    else{
                            $newword .= $str[$i];
                            $i++;
                            $j++;
                        }
                }while($j<$len);
            if($add){
                    return $newword.'...';
                }
            else
                return $newword;
        }

}




function mytheme_addmenus() {
	register_nav_menus(
		array(
			'main_nav' => 'The Main Menu',
		)
	);
}
add_action( 'init', 'mytheme_addmenus' );
 
function mytheme_nav() {
    if ( function_exists( 'wp_nav_menu' ) )
        wp_nav_menu( 'menu=main_nav&fallback_cb=mytheme_nav_fallback' );
    else
        mytheme_nav_fallback();
}
 
function mytheme_nav_fallback() {
	
    wp_page_menu( 'show_home=Home' );
}

if (function_exists('register_sidebar')) {
	register_sidebar(
		array(
			'before_widget' => '<div class="infosBloc">',
			'after_widget' => '</div>',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
			)
		);
}


function augMetro_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; 
	global $commentcount;
	
	if(!$commentcount) { //初始化楼层计数器

		$commentcount = 1;
	}
	
	?>
    
    <div id="comment-id-<?php comment_ID() ?>">
            	<div class="comment_text">
                <table align="center" border="0" cellpadding="0" cellspacing="0" width="623">
                    <tbody><tr>
                        <td valign="top" width="64"><?php  echo get_avatar( $comment, '64',get_bloginfo(stylesheet_directory).'/images/connectAvatar.png' ); ?>
						
						
                 </td>
                        <td style="padding-left: 21px;" valign="top">
                            <div class="nom_user">#<?=$commentcount++?>. <?php comment_author_link() ?>  |  <?php comment_date('F');?> ,<?php comment_time() ?></div>
                            <div class="float1"><a href='javascript:replyTo("#comment-<?php comment_ID() ?>","<?php comment_author()?>");' title="Reply to this comment" ><img src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/1292019345_sign-out.png" height="16" width="16"></a>  </div>
                            <div class="coment" style="min-height:35px;"><div id="comm-id-<?php comment_ID() ?>"><?php comment_text() ?></div></div>
                        </td>
                            </tr>
                    </tbody>
                </table>
            	</div>
            </div>
<?php
}

add_action('admin_menu', 'add_menu_settings');
function add_menu_settings (){
	if ( count($_POST) > 0 && isset($_POST['theme_settings']) ){
		$options = array ('SlideTitle1','SlideTitle2','SlideTitle3','SlideArticles1','SlideArticles2','SlideArticles3','SidebarAd1','SidebarAd2','PostBanner','Stretch1Title','Stretch1Content','Stretch2Title','Stretch2Content');
		foreach ( $options as $opt ){
			delete_option ( 'AMetro_'.$opt, $_POST[$opt] );
			add_option ( 'AMetro_'.$opt, $_POST[$opt] );
		}
	}

    if ($_POST['theme_settings']) echo '<div class="updated"><p><strong>Change Save !</strong></p></div>';
	add_menu_page('AMetro Setting', 'AMetro Setting', 'edit_themes', basename(__FILE__), 'AMetro_settings');
}
function aMetro_settings(){?>

<style type="text/css">
.form_field {
    clear: both;
    float: none;
    padding-left: 15px;
    padding-top: 7px;
    text-align: right;
    width: 400px;
}

.form_field strong {
    display: block;
    float: left;
    height: 13px;
    padding-top: 1px;
    text-align: left;
}

.form_field input {
    float: right;
    width: 200px;
}

.form_field textarea {
    float: right;
    width: 300px;
}

.fieldset {
   border:1px solid #ddd;
   padding-bottom:20px;
   margin-top:20px;
   width:500px;
}

.legend{
    margin-left:5px;
    padding:0 5px;
    color:#2481C6;
    /*text-transform:uppercase;*/
}

</style>

<div class="wrap">
	<h2>AMetro Theme Setting</h2>
	<form method="post" action="">
    <div>
    <fieldset class="fieldset">
        <legend class="legend"><strong>Sliders Setting</strong></legend>
        <div class="form_field" >
            <strong>SlideTitle 1</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_SlideTitle1')); ?>" name="SlideTitle1"  />
        </div>
        <div class="form_field" >
            <strong>SlideArticles 1</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_SlideArticles1')); ?>" name="SlideArticles1"  />
        </div>
        <div class="form_field" >
            <strong>SlideTitle 2</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_SlideTitle2')); ?>" name="SlideTitle2"  />
        </div>
        <div class="form_field" >
            <strong>SlideArticles 2</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_SlideArticles2')); ?>" name="SlideArticles2"  />
        </div>
        <div class="form_field" >
            <strong>SlideTitle 3</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_SlideTitle3')); ?>" name="SlideTitle3"  />
        </div>
        <div class="form_field" >
            <strong>SlideArticles 3</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_SlideArticles3')); ?>" name="SlideArticles3"  />
        </div>


   </fieldset>

   <fieldset class="fieldset">
        <legend class="legend"><strong>Sidebar Setting</strong></legend>
        <div class="form_field" >
            <strong>Box 1 Title</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_Stretch1Title')); ?>" name="Stretch1Title"  />
        </div>
        <div class="form_field" >
            <strong>Box 1 Content</strong><textarea name="Stretch1Content"  rows="6"><?php echo stripslashes(get_option('AMetro_Stretch1Content')); ?></textarea>
        </div>
        <div class="form_field" >
            <strong>Box 2 Title</strong><input type="text" value="<?php echo stripslashes(get_option('AMetro_Stretch2Title')); ?>" name="Stretch2Title"  />
        </div>
        <div class="form_field" >
            <strong>Box 2 Content</strong><textarea name="Stretch2Content"  rows="6"><?php echo stripslashes(get_option('AMetro_Stretch2Content')); ?></textarea>
        </div>



   </fieldset>

   <fieldset class="fieldset">
        <legend class="legend"><strong>AD Setting</strong></legend>
        <div class="form_field" >
         <strong>Sidebar AD 1</strong><textarea name="SidebarAd1"  rows="6"><?php echo stripslashes(get_option('AMetro_SidebarAd1')); ?></textarea>
        </div>

          Best size:215x215
        <div class="form_field" >
         <strong>Sidebar AD 2</strong><textarea name="SidebarAd2"  rows="6"><?php echo stripslashes(get_option('AMetro_SidebarAd2')); ?></textarea>
        </div>
        Best size:215x215
        <div class="form_field" >
         <strong>Post Banner</strong><textarea name="PostBanner"  rows="6"><?php echo stripslashes(get_option('AMetro_PostBanner')); ?></textarea>
        </div>
        Best size:728x90
   </fieldset>
   </div>
<!--<?php echo stripslashes(get_option('simple_analytics')); ?>-->
    <div style="float: none;">
		<p class="submit">
			<input type="submit" name="theme_settings" class="button-primary" value="Save" />

		</p>
        </div>
	</form>
</div>
<?php
    }

?>